#include <gtk/gtk.h>

// 对话框响应回调函数
static void on_response(GtkDialog *dialog, gint response_id, gpointer user_data) {
    if (response_id == GTK_RESPONSE_OK) {
        g_print("OK button clicked!\n");
    } else if (response_id == GTK_RESPONSE_CANCEL) {
        g_print("Cancel button clicked!\n");
    }
    gtk_widget_destroy(GTK_WIDGET(dialog)); // 关闭对话框
}

static void on_button_clicked(GtkWidget *button, gpointer parent_window) {
    // 创建对话框并指定父窗口
    GtkWidget *dialog = gtk_message_dialog_new(
        GTK_WINDOW(parent_window),
        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, // 模态对话框，销毁时同步销毁
        GTK_MESSAGE_INFO,
        GTK_BUTTONS_OK_CANCEL,
        "This is a simple dialog."
    );

    gtk_window_set_title(GTK_WINDOW(dialog), "Dialog Title");

    // 处理对话框的响应事件
    g_signal_connect(dialog, "response", G_CALLBACK(on_response), NULL);

    gtk_widget_show(dialog);
}


int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    // 创建主窗口
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    //gtk_window_set_title(GTK_WINDOW(window), "GTK Dialog Demo");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 200);

    // 设置窗口关闭事件
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    // 创建按钮
    GtkWidget *button = gtk_button_new_with_label("Open Dialog");
    gtk_container_add(GTK_CONTAINER(window), button);

    // 按钮点击事件
    g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), window);

    // 显示主窗口
    gtk_widget_show_all(window);

    gtk_main();
    return 0;
}

